import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Test {
    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> ans = new ArrayList<>();
        HashMap<Character,Integer> hash1 = new HashMap<>();
        for(int i = 0;i < p.length();i++) {
            //将字典的字符存入哈希表
            char c = p.charAt(i);
            hash1.put(c,hash1.getOrDefault(c,0) + 1);
        }
        int len = p.length();
        int n = s.length();
        HashMap<Character,Integer> hash2 = new HashMap<>();
        for(int left = 0,right = 0,count = 0;right < n;right++) {
            //进窗口
            char in = s.charAt(right);
            hash2.put(in,hash2.getOrDefault(in,0) + 1);
            if(hash2.get(in) <= hash1.getOrDefault(in,0)) count++;
            //判断
            if(right - left + 1 > len) {
                //出窗口
                char out = s.charAt(left);
                if(hash2.get(out) <= hash1.getOrDefault(out,0)) count--;
                hash2.put(out,hash2.get(out) - 1);
                left++;
            }
            //更新结果
            if(count == len) {
                ans.add(left);
            }
        }
        return ans;
    }
    public static void main(String[] args) {
        findAnagrams("aa","bb");
    }
}
